const log4js = require('../../../../utils/logUtils');
const logger = log4js.getLogger('sip');
const deviceCache = require("../../../../db/deviceCache");
const sip = require("sip");
const util = require("../../../../utils/stringUtils");
const sipUtils = require("../../../../utils/sipUtils");
const config = require("../../../../db/config");
const clientUtils = require("../clientUtils");

const TAG = "[向上级连]";
module.exports={
    async handle(req,content){
        const {upwardUri, self_account,upward } = await clientUtils.generateClientParams();
        sip.send({
            method: 'MESSAGE',
            status: 200, reason: 'OK',
            uri: upwardUri, version: config.sip.version,
            headers: {
                from: {uri: deviceCache.localUri, params: {tag: sipUtils.rstring()}},
                to: {uri: deviceCache.localUri, params: {tag: sipUtils.rstring()}},
                'call-id': sipUtils.rstring(),
                cseq: req.headers.cseq,
                'expires': '3600',
                'Max-Forwards': 70
            }
        }, function (ret) {

        });

        for (let catalog_index = 0; catalog_index < deviceCache.cameras.length; catalog_index++) {
            const deviceXml = this.getResponseXmlItem(catalog_index, upward.upper_account);

            sip.send({
                method: 'MESSAGE', uri: upwardUri, version: config.sip.version,
                headers: {
                    from: {uri: deviceCache.localUri, params: {tag: sipUtils.rstring()}},
                    to: {uri: deviceCache.localUri},
                    'call-id': sipUtils.rstring(),
                    cseq: req.headers.cseq,
                    'Content-Type': 'Application/MANSCDP+xml',
                    'expires': '3600',
                    'Max-Forwards': 70
                },
                content: '<?xml version="1.0" encoding="GB2312" ?>' +
                    '<Response>' +
                    '<CmdType>Catalog</CmdType>' +
                    '<SN>' + content.Query.SN + '</SN>' +
                    '<DeviceID>' + self_account + '</DeviceID>' +
                    '<SumNum>' + deviceCache.cameras.length + '</SumNum>' +
                    '<DeviceList Num="1">' +
                    deviceXml +
                    '</DeviceList></Response>'
            }, function (ret) {
                logger.info(TAG, '发送目录完成:', deviceCache.cameras[catalog_index].deviceid + "_" + deviceCache.cameras[catalog_index].channel_id,
                    deviceXml);
            }, function (err) {
                logger.error(TAG, '发送目录失败:', deviceCache.cameras[catalog_index].deviceid + "_" + deviceCache.cameras[catalog_index].channel_id,
                    deviceXml, err);
            });
        }
    },
    getResponseXmlItem(index,upper_account) {
        const tmp = deviceCache.cameras[index];
        let address = '', brand = '';
        if (tmp && tmp.brand) {
            brand = tmp.brand;
        }
        if (tmp && tmp.crmcop && tmp.crmcop.address) {
            address = tmp.crmcop.address;
        }
        return '<Item>' +
            '<DeviceID>' + tmp.channel_id + '</DeviceID>' +
            '<Name>' + util.normalize(tmp.description) + '</Name>' +
            '<Manufacturer>' + util.normalize(brand) + '</Manufacturer>' +
            '<Model>NO</Model>' +
            '<Owner>0</Owner>' +
            '<CivilCode>340200</CivilCode>' +
            '<Address>' + util.normalize(address) + '</Address>' +
            '<ParentID>' + upper_account + '</ParentID>' +
            '<RegisterWay>1</RegisterWay>' +
            '<Secrecy>0</Secrecy>' +
            '<StreamNum>2</StreamNum>' +
            '<Status>ON</Status>' +
            '<Info>' +
            '<PTZType>3</PTZType>' +
            '<DownloadSpeed>1/2/4/8</DownloadSpeed>' +
            '</Info>' +
            '</Item>';
    },
}
